package furn.config;


import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;

@SuppressWarnings("all")
@Configuration
@Slf4j
public class DruidDataSourceConfig {

    /**
     * 配置/注入 DruidDataSource对象
     * 1、当注入了DruidDataSource对象时，底层在检测时会检测到DruidDataSource对象，从而切换为DruidDataSource数据源
     * 2、假如没有注入其他的数据源对象，底层检测时检测不到数据源，从而切换为默认的HikariDataSource数据源
     * 3、@ConfigurationProperties(prefix = "spring.datasource") 读取application.yml配置文件中spring.datasource下的配置
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        log.info("当前数据源为: {}", druidDataSource.getClass());
        return druidDataSource;
    }
}
